<!--
 * @Descripttion: (工艺流程明细/Lm_RouteItem)
 * @Author: (admin)
 * @Date: (2024-10-22)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="RouteId" prop="routeId">
        <el-input v-model="queryParams.routeId" placeholder="请输入RouteId" />
      </el-form-item>
      <el-form-item label="ProcessId" prop="processId">
        <el-input v-model="queryParams.processId" placeholder="请输入ProcessId" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['lmrouteitem:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      >
      <el-table-column prop="routeId" label="RouteId" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('routeId')"/>
      <el-table-column prop="processId" label="ProcessId" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('processId')"/>
      <el-table-column prop="times" label="Times" align="center" v-if="columns.showColumn('times')"/>
      <el-table-column prop="processType" label="ProcessType" align="center" v-if="columns.showColumn('processType')">
        <template #default="scope">
          <dict-tag :options=" options.processTypeOptions" :value="scope.row.processType"  />
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="Operate" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('operate')"/>
      <el-table-column prop="testNum" label="TestNum" align="center" v-if="columns.showColumn('testNum')"/>
      <el-table-column prop="checkType" label="CheckType" align="center" v-if="columns.showColumn('checkType')">
        <template #default="scope">
          <dict-tag :options=" options.checkTypeOptions" :value="scope.row.checkType"  />
        </template>
      </el-table-column>
      <el-table-column prop="spotCheck" label="SpotCheck" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('spotCheck')"/>
      <el-table-column prop="material" label="Material" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('material')"/>
      <el-table-column prop="deviceID" label="DeviceID" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('deviceID')"/>
      <el-table-column prop="createBy" label="CreateBy" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('createBy')"/>
      <el-table-column prop="createTime" label="CreateTime" :show-overflow-tooltip="true"  v-if="columns.showColumn('createTime')"/>
      <el-table-column prop="updateBy" label="UpdateBy" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('updateBy')"/>
      <el-table-column prop="updateTime" label="UpdateTime" :show-overflow-tooltip="true"  v-if="columns.showColumn('updateTime')"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['lmrouteitem:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['lmrouteitem:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">

          <el-col :lg="12">
            <el-form-item label="RouteId" prop="routeId">
              <el-input v-model="form.routeId" placeholder="请输入RouteId" :disabled="opertype != 1"/>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="ProcessId" prop="processId">
              <el-input v-model="form.processId" placeholder="请输入ProcessId" :disabled="opertype != 1"/>
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="Times" prop="times">
              <el-input v-model.number="form.times" placeholder="请输入Times" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="ProcessType" prop="processType">
              <el-select v-model="form.processType"  placeholder="请选择ProcessType">
                <el-option
                  v-for="item in options.processTypeOptions" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="Operate" prop="operate">
              <el-input v-model="form.operate" placeholder="请输入Operate" />
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="TestNum" prop="testNum">
              <el-input v-model.number="form.testNum" placeholder="请输入TestNum" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="CheckType" prop="checkType">
              <el-select v-model="form.checkType"  placeholder="请选择CheckType">
                <el-option
                  v-for="item in options.checkTypeOptions" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="SpotCheck" prop="spotCheck">
              <el-input v-model="form.spotCheck" placeholder="请输入SpotCheck" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="Material" prop="material">
              <el-input v-model="form.material" placeholder="请输入Material" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="DeviceID" prop="deviceID">
              <el-input v-model="form.deviceID" placeholder="请输入DeviceID" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="CreateBy" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入CreateBy" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="CreateTime" prop="createTime">
              <el-date-picker
                v-model="form.createTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="UpdateBy" prop="updateBy">
              <el-input v-model="form.updateBy" placeholder="请输入UpdateBy" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="UpdateTime" prop="updateTime">
              <el-date-picker
                v-model="form.updateTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="lmrouteitem">
import { listLmRouteitem,
 addLmRouteitem, delLmRouteitem, 
 updateLmRouteitem,getLmRouteitem, 
 } 
from '@/api/craft/lmrouteitem.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'RouteId',
  sortType: 'asc',
  routeId: undefined,
  processId: undefined,
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'routeId', label: 'RouteId'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'processId', label: 'ProcessId'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'times', label: 'Times'   },
  { visible: true, align: 'center', type: 'dict', prop: 'processType', label: 'ProcessType'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'operate', label: 'Operate'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'testNum', label: 'TestNum'   },
  { visible: true, align: 'center', type: 'dict', prop: 'checkType', label: 'CheckType'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'spotCheck', label: 'SpotCheck'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'material', label: 'Material'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'deviceID', label: 'DeviceID'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'createBy', label: 'CreateBy'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'createTime', label: 'CreateTime'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'updateBy', label: 'UpdateBy'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'updateTime', label: 'UpdateTime'  ,showOverflowTooltip: true  },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])
var dictParams = []

function getList(){
  loading.value = true
  listLmRouteitem(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    routeId: [{ required: true, message: "RouteId不能为空", trigger: "blur"     }],
    processId: [{ required: true, message: "ProcessId不能为空", trigger: "blur"     }],
    processType: [{ required: true, message: "ProcessType不能为空", trigger: "change"     }],
  },
  options: {
    // ProcessType 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
processTypeOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    routeId: null,
    processId: null,
    times: null,
    processType: null,
    operate: null,
    testNum: null,
    checkType: null,
    spotCheck: null,
    material: null,
    deviceID: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加工艺流程明细'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.routeId || ids.value
  getLmRouteitem(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改工艺流程明细'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.routeId != undefined && opertype.value === 2) {
        updateLmRouteitem(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLmRouteitem(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.routeId || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delLmRouteitem(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}

handleQuery()
</script>